<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SMS
 *
 * @author Khoi
 */
class SMS {

    public $phoneNumber;
    public $text;

    public function __construct($_phoneNumber, $_text) {
        $this->phoneNumber = $_phoneNumber;
        $this->text = $_text;
    }

    public function send() {
        $serial = new phpSerial;
        $serial->deviceSet('COM1');

        // Then we need to open it
        $serial->deviceOpen();

        // To write into
        $serial->sendMessage("AT+CMGF=1\r\n");
        $serial->sendMessage("AT+CMGS=\"$this->phoneNumber\"\r\n");
        $serial->sendMessage("$this->text\r\n");
        $serial->readPort();
        $serial->sendMessage(chr(26));
        //wait for modem to send message
        $status = '';
        while (!strstr($status, "OK\r\n") && !strstr($status, "ERROR\r\n")) {
            while (!($buffer = $serial->readPort()) || $buffer == chr(26) || strstr($buffer, '+CMGS')) {
                usleep(500);
            }
            $status .= $buffer;
        }
        $serial->deviceClose();
        return $status;
    }

}
